package com.blogs.component;

import java.util.ArrayList;

import com.blogs.entity.IngFeed;
import com.blogs.tools.DBSharedPreferences;
import com.blogs.tools.FormatTime;
import com.blogs.tools.HtmlFormat;
import com.blogs.tools.ImageDownLoad;
import com.blogs.ui.IngDetail;
import com.blogs.ui.IngPublicActivity;
import com.cnblogs.R;
import com.cnblogs.R.color;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class IngFeedAdapter extends BaseAdapter {
	private ViewHolder holder;
	private ArrayList<IngFeed> feedList = null;
	private Activity context = null;
	private DBSharedPreferences sp;

	public IngFeedAdapter(Activity context, ArrayList<IngFeed> feedList) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.feedList = feedList;
		sp = new DBSharedPreferences(context);
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return feedList.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return feedList.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.ing_adapter_template, null);
			holder = new ViewHolder();
			holder.ing_avatar = (ImageView) convertView.findViewById(R.id.ing_avatar);
			holder.int_mask = (ImageView) convertView.findViewById(R.id.int_mask);
			holder.ing_author = (TextView) convertView.findViewById(R.id.ing_author);
			holder.ing_time = (TextView) convertView.findViewById(R.id.ing_time);
			holder.ing_con = (TextView) convertView.findViewById(R.id.ing_con);
			holder.ing_com_body = (LinearLayout) convertView.findViewById(R.id.ing_com_body);
			holder.ing_has_more_com = (TextView) convertView.findViewById(R.id.ing_has_more_com);
			for (int i = 0; i < 15; i++) {
				View comView = View.inflate(context, R.layout.ing_com_template, null);
				ComViewHolder comHolder = new ComViewHolder();
				comHolder.ing_com_text_tv = (TextView) comView.findViewById(R.id.ing_com_text_tv);
				comView.setTag(comHolder);
				holder.ing_com_body.addView(comView);
			}
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final int p = position;
		int com_size = feedList.get(position).com_feeds.size();
		if (com_size > 0) {
			holder.ing_com_body.setVisibility(View.VISIBLE);
		} else {
			holder.ing_com_body.setVisibility(View.GONE);
		}
		for (int i = 0; i < 15; i++) {
			View comView = holder.ing_com_body.getChildAt(i);
			comView.setVisibility(View.GONE);
			if (i < com_size) {
				comView.setVisibility(View.VISIBLE);
				ComViewHolder comHolder = (ComViewHolder) comView.getTag();

				final int pi = i;
				comView.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent hintent = new Intent();
						hintent.setClass(context, IngPublicActivity.class);
						hintent.putExtra("position", p);
						hintent.putExtra("AtAuthor", feedList.get(p).com_feeds.get(pi).author);
						hintent.putExtra("ContentId", Integer.parseInt(feedList.get(p).id));
						hintent.putExtra("ReplyTo",
								Integer.parseInt(feedList.get(p).com_feeds.get(pi).author_id == null ? "0" : feedList
										.get(p).com_feeds.get(pi).author_id));
						hintent.putExtra("ParentCommentId", Integer.parseInt(feedList.get(p).com_feeds.get(pi).id));
						hintent.putExtra("FromMsg", feedList.get(p).com_feeds.get(pi).content);
						context.startActivity(hintent);
						Log.i("comViewClick", feedList.get(p).com_feeds.get(pi).author);
					}
				});
				String text = "<font color=#6699cc >" + feedList.get(position).com_feeds.get(i).author + "</font>:  ";
				text += HtmlFormat.FormatIngAt(feedList.get(position).com_feeds.get(i).content);
				text += "  <font color=#c2c2c2 >"
						+ FormatTime.formatShort(feedList.get(position).com_feeds.get(i).com_time) + "</font>";

				comHolder.ing_com_text_tv.setText(Html.fromHtml(text));
			}
		}
		if (feedList.get(position).has_more_com == 1) {
			holder.ing_has_more_com.setVisibility(View.VISIBLE);
			holder.ing_has_more_com.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent mintent = new Intent();
					mintent.setClass(context, IngDetail.class);
					mintent.putExtra("position", p);
					mintent.putExtra("ingid", feedList.get(p).id);
					mintent.putExtra("author", feedList.get(p).author);
					mintent.putExtra("author_id", feedList.get(p).author_id);
					mintent.putExtra("avatar", feedList.get(p).avatar);
					mintent.putExtra("com_time", feedList.get(p).com_time);
					mintent.putExtra("body", feedList.get(p).body);
					context.startActivity(mintent);
				}
			});
		} else {
			holder.ing_has_more_com.setVisibility(View.GONE);
		}
		holder.ing_avatar.setImageResource(R.drawable.sample_face);
		if (sp.GetIsShowPic()) {
			ImageDownLoad.loadDrawable(feedList.get(position).avatar, holder.ing_avatar);
		}
		if (sp.GetIsDarkState()) {
			holder.int_mask.getBackground().setLevel(2);
			holder.ing_com_body.getBackground().setLevel(2);
			holder.ing_con.setTextColor(context.getResources().getColor(color.con_butom_night));
		} else {
			holder.int_mask.getBackground().setLevel(1);
			holder.ing_com_body.getBackground().setLevel(1);
			holder.ing_con.setTextColor(Color.parseColor("#000000"));
		}
		holder.ing_author.setText(Html.fromHtml(feedList.get(position).author));
		holder.ing_time.setText(FormatTime.formatShort(feedList.get(position).com_time));
		holder.ing_con.setText(Html.fromHtml(feedList.get(position).body));
		if(feedList.get(position).lucky==1)
		{
			holder.ing_con.append(Html.fromHtml("<img src='"+R.drawable.ing_lucky+"'/>", new MyImageGetter(context,5,-3), null));
		}
		if(feedList.get(position).newbie==1)
		{
			holder.ing_con.append(Html.fromHtml("<img src='"+R.drawable.newbie+"'/>", new MyImageGetter(context,8,-3), null));
		}
		return convertView;
	}

	class ViewHolder {
		ImageView ing_avatar;
		TextView ing_author;
		TextView ing_time;
		TextView ing_con;
		LinearLayout ing_com_body;
		TextView ing_has_more_com;
		ImageView int_mask;
	}

	class ComViewHolder {
		TextView ing_com_text_tv;
	}
}
